package graph.day14;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Leetcode_1002 {

    public static List<String> commonChars(String[] words) {
        int[] common = new int[26];
        Arrays.fill(common, 10001);
        int[] tmp = new int[26];
        for (String word : words) {
            Arrays.fill(tmp, 0);
            for (int i = 0; i < word.length(); i++) {
                tmp[word.charAt(i) - 'a']++;
            }
            for (int i = 0; i < common.length; i++) {
                common[i] = Math.min(common[i], tmp[i]);
            }
        }
        List<String> result = new ArrayList<>();
        for (int i = 0; i < common.length; i++) {
            while (common[i] > 0) {
                result.add(String.valueOf((char) (i + 'a')));
                common[i]--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String[] words = {"bella", "label", "roller"};
        System.out.println(commonChars(words));
    }
}
